package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.bean.StepBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * TabActivity use to classify a Recipe
 * @author Younes Chandarli et Christophe Chillan
 *
 */
public class MenuTabIngredient extends TabActivity {
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.menutab);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  
	    
	    
	    Bundle extras = getIntent().getExtras(); 
	    if(extras !=null)
	    {
	    	intent = new Intent().setClass(this, RecipeView.class);
	    	final RecipeBean recipe = (RecipeBean) getIntent().getExtras().getSerializable("recipe");
	    	List<StepBean> steps = RecipesDB.getStepDao().getStepsFromRecipeId(RecipesDB.getDatabase(), recipe.getId());
	    	Bundle bundle = new Bundle();
			bundle.putSerializable("recipe", recipe);
			intent.putExtras(bundle);
	    	// Initialize a TabSpec for each tab and add it to the TabHost
		    spec = tabHost.newTabSpec("Détail des recettes").setIndicator("Recette",
		                      res.getDrawable(R.drawable.list))
		                  .setContent(intent);
		    tabHost.addTab(spec);
		    // Do the same for the other tabs
		    intent = new Intent().setClass(this, IngredientView.class);
		    intent.putExtras(bundle);
		    spec = tabHost.newTabSpec("ingredients").setIndicator("Ingredients",
		                      res.getDrawable(R.drawable.ingredient))
		                  .setContent(intent);
		    tabHost.addTab(spec);
		    if(steps.size()>0){
		    	intent = new Intent().setClass(this, StepView.class);
		    	bundle.putSerializable("steps", new ArrayList<StepBean>(steps));
		    	intent.putExtras(bundle);
		    	spec = tabHost.newTabSpec("steps").setIndicator("Etapes",
		                      res.getDrawable(R.drawable.step))
		                  .setContent(intent);
		    	tabHost.addTab(spec);
		    }
	    }
	    
	}

}
